package com.clas.liu.myapplication.view.activity.impl;

import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import com.clas.liu.myapplication.MyApplication;
import com.clas.liu.myapplication.presenter.i.IBasePresenter;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;

/**
 * activity的基类
 * Created by liu on 2019/2/27.
 */

public abstract class BaseActivity <P extends BasePresenter & IBasePresenter>
        extends AppCompatActivity{

    protected P mPresenter;
    private Toast mToast;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ((MyApplication)getApplication()).addActivity_(this);
        mPresenter = getPresenter();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //强制竖屏
    }

    protected abstract P getPresenter();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ((MyApplication)getApplication()).removeActivity_(this);
        if (mPresenter!=null){
            mPresenter.onDestroy();
            mPresenter = null;
        }
    }

    /**
     * 设置状态栏透明,适用于非DrawerLayout
     */
    public void setStatusBarTransparent() {
            //设置状态栏透明
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //设置跟布局的参数,让布局从状态栏下方开始,而不是跟状态栏重合
            ViewGroup rootView = (ViewGroup) ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(false);
            rootView.setClipToPadding(false);
    }
    public void showToast(String message,int timeType){
        if(mToast == null){
            if(timeType == Toast.LENGTH_LONG)
                mToast = Toast.makeText(this,message,Toast.LENGTH_LONG);
            else
                mToast = Toast.makeText(this,message,Toast.LENGTH_SHORT);
        }else{
            mToast.setText(message);
            mToast.setDuration(timeType);
        }
        mToast.show();
    }
}
